package com.ktgj.flight.common.ruili;

import com.ktgj.common.core.text.Convert;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;

/**
 * aes加密
 */
public class AES {
    /**
     * AES 加密
     * @param str
     * @param key
     * @return
     */
    public static String AesEncrypt(String str, String key)
    {
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            kgen.init(128, new SecureRandom(key.getBytes()));
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec ky = new SecretKeySpec(enCodeFormat, "AES");
            Cipher cipher = Cipher.getInstance("AES");// 创建密码器
            byte[] byteContent = str.getBytes("utf-8");
            cipher.init(Cipher.ENCRYPT_MODE, ky);// 初始化
            byte[] result = cipher.doFinal(byteContent);
            return byteToHexStr(result); // 加密
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        /*if (Strings.isNullOrEmpty(str)) return null;
        char[] toEncryptArray = str.toCharArray();

        System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
        {
            Key = StringToBinStr(key),
                    Mode = System.Security.Cryptography.CipherMode.ECB,
                    Padding = System.Security.Cryptography.PaddingMode.PKCS7
        };

        System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
        byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.length);

        return byteToHexStr(resultArray);*/
    }

    /**
     * 解密
     * @param str
     * @param key
     * @return
     */
    public static String AesDecrypt(String str, String key)
    {
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            kgen.init(128, new SecureRandom(key.getBytes()));
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec ky = new SecretKeySpec(enCodeFormat, "AES");
            Cipher cipher = Cipher.getInstance("AES");// 创建密码器
            cipher.init(Cipher.DECRYPT_MODE, ky);// 初始化
            byte[] result = cipher.doFinal(StringToBinStr(str));
            return byteToHexStr(result); // 解密
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        /*byte[] toEncryptArray = StringToBinStr(str);
        byte[] keyArray = StringToBinStr(key);

        System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
        {
            Key = keyArray,
                    Mode = System.Security.Cryptography.CipherMode.ECB,
                    Padding = System.Security.Cryptography.PaddingMode.PKCS7
        };

        System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor();
        byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.length);

        return UTF8Encoding.UTF8.GetString(resultArray);*/
    }

    /**
     * 字节数组转16进制字符串
     * @param bytes
     * @return
     */
    public static String byteToHexStr(byte[] bytes)
    {
        StringBuffer strbuf = new StringBuffer(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            strbuf.append(((int)bytes[i] & 0xff));
        }
        return strbuf.toString();

        /*StringBuilder strbuf = new StringBuilder(bytes.length * 2);
        int i;
        for (i = 0; i < bytes.length; i++)
        {
            strbuf.Append(((int)bytes[i] & 0xff).toString("X2"));
        }
        return strbuf.toString();*/
    }

    /**
     * 字符串转二进制字节数组
     * @param src
     * @return
     */
    public static byte[] StringToBinStr(String src)
    {
        if (src.length() < 1)
            return null;
        byte[] encrypted = new byte[src.length() / 2];
        for (int i = 0; i < src.length() / 2; i++)
        {
            int high = Convert.toInt(src.substring(i * 2, 1), 16);
            int low = Convert.toInt(src.substring(i * 2 + 1, 1), 16);
            encrypted[i] = (byte)(high * 16 + low);
        }
        return encrypted;
    }
}
